package com.sky.utils;

import java.lang.reflect.Field;

public class ElVueGenerateUtils {
    public static void getElTable(Class classz) {
        Field[] fields = classz.getDeclaredFields();
        System.out.println("--------------------el-table-------------------");
        System.out.println("<el-table");
        System.out.println("\t:data=\"tableData\"");
        System.out.println("\tstyle=\"width: 100%\"");
        System.out.println("\tborder>");
        for (int i = 0; i < fields.length; i++) {
            String fieldName = fields[i].getName();
            System.out.println(getElTableColumn(fieldName));
        }
        System.out.println(getElTableColumnOperate());
        System.out.println("</el-table>");
    }

    private static String getElTableColumn(String name) {
        return "\t<el-table-column prop=\"" + name + "\" label=\"" + name + "\" header-align=\"center\" align=\"center\"></el-table-column>";
    }

    private static String getElTableColumnOperate() {
        String result = "\t<el-table-column label=\"操作\" header-align=\"center\" align=\"center\">\n" +
                "\t\t<template slot-scope=\"scope\">\n" +
                "\t\t\t<el-button type=\"primary\" size=\"mini\" @click=\"handleEdit(scope.row.id)\">编辑</el-button>\n" +
                "\t\t\t<el-button type=\"danger\" size=\"mini\" @click=\"handleDelete(scope.row.id)\">删除</el-button>\n" +
                "\t\t</template>\n" +
                "\t</el-table-column>";
        return result;
    }

    private static String getElFormItem(String name) {
        return "\t<el-form-item label=\"" + name + ">" + "\n" +
                "\t\t<el-input v-model=\"form." + name + "\"></el-input>" + "\n" +
                "\t</el-form-item>";
    }

    public static void getElForm(Class classz) {
        Field[] fields = classz.getDeclaredFields();
//        System.out.println("--------------------el-form-------------------");
        System.out.println("<el-form :model=\"form\" :rules=\"rules\" ref=\"form\" label-width=\"80px\" class=\"demo-ruleForm\">");
        for (int i = 0; i < fields.length; i++) {
            String fieldName = fields[i].getName();
            System.out.println(getElFormItem(fieldName));
        }
        System.out.println("</el-form>");
    }

    public static void getElDialog(Class classz) {
        System.out.println("--------------------el-dialog-------------------");
        System.out.println("<el-dialog :title=\"dialogTitle\" :visible.sync=\"dialogVisible\" width=\"70%\">");
        getElForm(classz);
        System.out.println("\t<div slot=\"footer\" class=\"dialog-footer\">\n" +
                "\t\t<el-button @click=\"dialogVisible = false\">取 消</el-button>\n" +
                "\t\t<el-button type=\"primary\" @click=\"submitForm('form')\">确 定</el-button>\n" +
                "\t</div>");
        System.out.println("</el-dialog>");
    }
}
